package com.bd.rj.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.rj.backstage.mapper.InterviewMapper;
import com.bd.rj.common.entity.Post;
import com.bd.rj.backstage.mapper.PostMapper;
import com.bd.rj.backstage.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 白世俊
 * @since 2025-01-03
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private InterviewMapper interviewMapper;

    @Override
    public IPage<Post> getPostByPage(int pageNum,int pageSize, QueryWrapper<Post> queryWrapper) {
        Page<Post> page = new Page(pageNum, pageSize);
        page = this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }


    @Override
    public String deletePost(Long id) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Post::getId,id);
        boolean result = this.remove(queryWrapper);
        return result?"成功":"失败";
    }

    @Override
    public String addPost(Post post) {
        post.setStatus(0);
        post.setCreatetime(new Date());
        boolean result = this.save(post);
        return result?"成功":"失败";
    }

    @Override
    public String updatePost(Post post) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Post::getId,post.getId());
        boolean result = this.update(post,queryWrapper);
        return result?"成功":"失败";
    }

    public Map<String, Object> getWeeklyPost() {
       Map<String, Object> weeklyPosts = postMapper.getWeeklyPosts();
        return weeklyPosts;
    }

    @Override
    public List<Map<String, Object>> getPostTypePer() {
        List<Map<String, Object>> postTypePer = postMapper.getPostTypePer();
        return postTypePer;
    }

    @Override
    public void insertPostRecord(Map<String, Object> postRecord) {
        postMapper.insertPostRecord(postRecord);
    }

    @Override
    public IPage<Map> getEnterpriseByConditionAndPage(int pagenum, int pageSize, Post post) {
       IPage<Map> page = new Page<>(pagenum,pageSize);
       return postMapper.query(page,post);
    }

    @Override
    public int getPostCount(Long uid) {
        return postMapper.getPostCount(uid);
    }

    @Override
    public List<Map<String, Object>> getPostPercentage(Long uid) {
        return postMapper.getPostPercentage(uid);
    }




    @Override
    public Map<String, Object> queryId(Long id) {
        // 使用 MyBatis-Plus 的 selectById 方法根据 id 获取 Post 对象
        Post post = postMapper.selectById(id);

        // 使用自定义方法统计 interview 表中满足条件的记录数
        int interviewCount = interviewMapper.countInterviewsByPidAndStatus(id);
        post.setInterviewCount(interviewCount);

        // 获取 interview 列表，如果你需要，可以添加此功能
        // List<Interview> interviewList = interviewMapper.selectList(wrapper);
        // post.setInterviewList(interviewList);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data", post);
        return map;
    }
}


